<?php

register_shutdown_function(function() {
  $lastError = error_get_last();
  if (!is_null($lastError)) {
    $_SESSION['error'] = print_r($lastError, true);
  }
});

include 'config.inc.php';
include _ITSTOCK_LIB_DIR_.'save.inc.php';

itstock_session_start();

function save($data, $type) {

  if (!itstock_structure($structure, $type)) {
    $_SESSION['error'] = "Error: Can't find type: $type";
  }

  $structure['data'][0] = &$data;
  itstock_decode_list_id($structure);
  if (itstock_save($structure)) {
    $_SESSION['msg'] = 'Saved Successfuly.';
  } else {
    $_SESSION['error'] = (array_key_exists($structure['errno'], $GLOBALS['DB_ERRORS'])? sprintf($GLOBALS['DB_ERRORS'][$structure['errno']],$structure['type']):$structure['msg']);
  }
  itstock_db_close_all();
  header('Location: '.$_SESSION['on_save'][$type]);
}

if ((isset($_POST["id"]) || isset($_POST['data'][0]['id'])) && isset($_GET["type"])) {
    if (!itstock_valid_login($_SESSION['user'])) {
        header("Location: index.php");
        exit();
    }
    save($_POST, $_GET["type"]);
    exit();
}

header("Location: index.php");
?>
